<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="850px"
  >
    <div class="form-box">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="选择游戏" prop="gameId" >
          <el-select :disabled="isEdit" v-model="form.gameId" placeholder="请选择游戏" clearable filterable @change="selectGameType">
              <el-option v-for="dict in allGameList" :key="dict.id" :label="dict.name" :value="dict.id" />
            </el-select>
        </el-form-item>
        
        <el-form-item label="图标" prop="icon" :class="{'hiddenupload':imgShowList.length}">
          <el-upload
                  :action="uploadImgUrl"
                  :file-list="imgShowList"
                  :show-file-list="true"
                  list-type="picture-card"
                  :limit="1"
                  :on-success="handleUploadSuccess"
                  :on-remove="handleRemove"
                  :on-preview="handlePictureCardPreview"
                >
                  <i class="el-icon-plus"></i>
                </el-upload>
        </el-form-item>
        <el-form-item label="排序" prop="sort" v-if="!(form.decorationTypeArea==1||form.decorationTypeArea==3)">
          <el-input v-model="form.sort" placeholder="请输入排序" type="number"/>
        </el-form-item>
        <!-- <el-form-item label="启用状态">
          <el-radio-group v-model="form.state">
            <el-radio  :label="1">启用</el-radio>
            <el-radio  :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import gameSettingApi from '@/api/tenantApis/gameSetting'
export default {
  name: "",
  components: {},
  data() {
    return {
      dialogVisible: false,
      loading: false,
      title: "新增",
      isEdit: false,
      imgShowList: [],
      // 表单参数
      form: {
        gameId:'',
          icon:'',
          id:'',
          state:1,
      },
      rules: {
        icon:[
          { required: true, message: "请上传图片", trigger: "change" }
        ],
      }
    };
  },
  computed:{
    allGameList(){
      return this.$store.getters.allGameList
    }
  },
  created() {
    this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/image";
    
  },
  methods: {
   
    selectGameType(e){
      if(e){
        this.allGameList.forEach(element => {
          if(element.id==e){
            this.form.icon = element.icon
            this.imgShowList = [{ url: element.icon, name: "" }];
          }
        });
      }
    },
    // 打开弹窗
    open(flag, data,type) {
      this.imgShowList = [];
      
      if (flag == "edit") {
        this.title = "修改";
        this.isEdit = true;
        this.form = JSON.parse(JSON.stringify(data))
        this.form.gameId = data.id
        this.form.id = data.tenantGameInfoId
        this.imgShowList = [{ url: data.icon, name: "" }]
      } else {
        this.form={
          gameId:'',
          icon:'',
          id:'',
          state:1,
        }
        this.isEdit = false;
      }
      this.dialogVisible = true;
    },
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    submitForm(formName) {
      this.$refs["" + formName].validate(valid => {
        if (valid) {
          if (this.form.id) {
            // 编辑
            gameSettingApi.editGame(this.form).then(res => {
              this.$message.success(res.msg);
              this.dialogVisible = false;
              this.$parent.getList();
            });
          } else {
            // 新增
            gameSettingApi.addGame(this.form).then(res => {
              this.$message.success(res.msg);
              this.dialogVisible = false;
              this.$parent.getList();
            });
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },
    // 图片上传成功
    handleUploadSuccess(res, file, fileList) {
      if (res.code == 200) {
        // 取得新上传的文件
        this.imgShowList = [{ url: res.data.filePath, name: "" }];
          this.form.icon  = res.data.filePath
      }
    },
    // 详情图片删除
    handleRemove(response, fileList, file) {
      this.imgShowList = []
      this.form.icon = ''
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url]
      });
    },
    uploadNumber(file, fileList) {
      //限制图片数量
      this.$modal.msgError("最多只能上传1张图片");
    },
    close() {
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped lang="css">
</style>
